package com.peaksport.pkms.learntest.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.core.MediaType;

import org.apache.servicecomb.provider.rest.common.RestSchema;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.google.common.collect.Maps;
import com.peaksport.framework.base.controller.BaseController;
import com.peaksport.framework.extend.redis.annotation.PKCacheEvict;
import com.peaksport.framework.extend.redis.annotation.PKCachePut;
import com.peaksport.framework.extend.redis.annotation.PKCacheable;
import com.peaksport.framework.extend.redis.venum.ExpireTimeType;
import com.peaksport.pkms.oauth.vo.TokenVO;
import com.peaksport.pkms.permit.entity.UserEntity;

@RestSchema(schemaId="pkms-test-redisCache")
@RequestMapping(value="test/testRedis",produces=MediaType.APPLICATION_JSON)
public class TestRedisCacheController extends BaseController {
	
	
	@SuppressWarnings("unchecked")
	@GetMapping("/testSaveStr")
	public void testSaveString() {
		//pkRedisTemplate.setString("str", "匹克1");
		UserEntity u = new UserEntity();
		u.setId("userIdxxxx");
		u.setName("郭锦彬");
		pkRedisTemplate.boundHashOps("hashTest2").put("first", "guojbin");
		pkRedisTemplate.setExpireTime("hashTest2", 1000);
		//pkRedisTemplate.boundHashOps("hashTest1").put("sec", u);
		
	}
	
	
	@GetMapping("testGetStr")
	public String testGetString() {
		return pkRedisTemplate.getString("str");
	}
	@GetMapping("testSaveEntity")
	public void testSaveEntity() {
		UserEntity u = new UserEntity();
		u.setId("userIdxxxx");
		u.setName("郭锦彬");
		pkRedisTemplate.setObject("entity", u);
		pkRedisTemplate.setExpireTime("entity", 1000L);
		
	}
	@GetMapping("testGetEntity")
	public UserEntity testGetEntity() {
		UserEntity u = (UserEntity) pkRedisTemplate.getObject("entity");
		return u;
	}
	@GetMapping("testCacheable/{name}")
	@Cacheable(value="pkms_expireTime_60s") //ExpireTimeType.expireTime_60s)
	public String testCacheable(@PathVariable("name") String name) {
		TimeUnit.HOURS.toSeconds(1);
		return "匹克: " + name + "==" + TimeUnit.HOURS.toSeconds(1);
	}
	@GetMapping("testCacheableCustomCacheName")
	@PKCacheable(expireTimeType=ExpireTimeType.expireTime_1h)
	public String testCacheableCustomCacheName() {
		return "泉州";
	}
	@GetMapping("testCacheableEntity")
	@PKCacheable()
	public UserEntity testCacheableEntity() {
		UserEntity u = new UserEntity();
		u.setId("userIdyyyy");
		u.setName("王老王");
		return u;
	}
	
	@GetMapping("testCacheableSameExpire1")
	@PKCacheable(expireTimeType=ExpireTimeType.expireTime_60s)
	public String testCacheableSameExpire1(){
		return "aa老王abbb";
	}
	@GetMapping("testCacheableSameExpire2")
	@PKCacheable(expireTimeType=ExpireTimeType.expireTime_60s)
	public String testCacheableSameExpire2(){
		return "aa老王abbb";
	}
	
	@GetMapping("testCacheCutExpireTime")
	@PKCachePut(expireTimeType=ExpireTimeType.expireTime_5min)
	public String testCacheCutExpireTime(){
		return "aa老王ae";
	}
	
	
	@GetMapping("testCacheCutEntity")
	@PKCachePut(expireTimeType=ExpireTimeType.expireTime_5min)
	public UserEntity testCacheCutEntity(){
		UserEntity u = new UserEntity();
		u.setId("xxxadgdfa");
		u.setName("大匹克");
		return u;
	}
	@GetMapping("testCacheEvict")
	@PKCacheEvict(expireTimeType=ExpireTimeType.expireTime_5min)
	public String testCacheEvict() {
		return "徐金秋";
	}
	@GetMapping("testCacheEvictEntity")
	@PKCacheEvict(expireTimeType=ExpireTimeType.expireTime_5min, allEntries=true)
	public UserEntity testCacheEvictEntity() {
		UserEntity u = new UserEntity();
		u.setId("chinapeak");
		u.setName("习近平");
		return u;
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@GetMapping("testHashExpire")
	public Map<String,Object> testHashExpire() {
		Map map = Maps.newHashMap();

		map.put("11", "1#");
		map.put("22", "2#");
		map.put("33", "2#");
		
		pkRedisTemplate.putAllHash("hashMapE", map);
		return map;
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@GetMapping("testListExpire")
	public List testListExpire() {
		UserEntity u = new UserEntity();
		u.setId("chinapeak");
		u.setName("习近平");
		List lst = new ArrayList<>();
		lst.add("lst1");
		lst.add("lst2");
		lst.add("lst3");
		lst.add("lst4");
		lst.add("lst5");
		lst.add(u);
		pkRedisTemplate.putAllList("listExpire", lst);
		List newLst = pkRedisTemplate.boundListOps("listExpire").range(0, -1);
		return newLst;
		
	}
	@GetMapping("sessionId")
	public String getSessionId(HttpServletRequest httpRequest) {
		return "sessionid is null";
	}
	
	@GetMapping("testRunOtherService")
	public TokenVO testRunOtherService() {
		String Service_permit = "pmks-permit";
		TokenVO tokenVo = new TokenVO();//.getForObject("cse://pkms-permit/pkOAuth/vaildToken?token={token}", TokenVO.class, "ximEBbHdQrKL26LI9SYegSPeF2w=");
		//ResponseEntity<TokenVO> tokenVo = restTemplate.getForEntity("cse://pkms-permit/pkOAuth/vaildToken?token={token}", TokenVO.class, "ximEBbHdQrKL26LI9SYegSPeF2w=");
		tokenVo.setClientId("aaa");
		return tokenVo;
	}
	
}

